package com.bilgeadam.conversationmanagement.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bilgeadam.conversationmanagement.model.Customer;

/**
 *
 * @author Bora SAYINER
 * @since Dec 28, 2014 6:18:08 PM
 * @version 1.0
 */
public class HiddenFieldServlet extends HttpServlet {

	private List<Customer> customers;

	public HiddenFieldServlet() {
		this.customers = new ArrayList<Customer>();
	}

	@Override
	public void init() throws ServletException {
		customers.add(new Customer(1, "Ozan", "YAMAN"));
		customers.add(new Customer(2, "Haluk", "KELLECİOĞLU"));
		customers.add(new Customer(3, "HAKAN", "AKÖZ"));
		customers.add(new Customer(4, "Emrah", "DOĞAN"));
		customers.add(new Customer(5, "Bora", "SAYINER"));
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String uri = req.getRequestURI();
		if (uri.endsWith("hiddenField")) {
			printShowCustomersPage(resp);
		} else if (uri.endsWith("editCustomer")) {
			printEditCustomersPage(req, resp);
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		int id = Integer.valueOf(req.getParameter("id"));
		Customer customer = findCustomerById(id);
		customer.setFirstName(req.getParameter("firstName"));
		customer.setLastName(req.getParameter("lastName"));
		printShowCustomersPage(resp);
	}

	private void printEditCustomersPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		int id = Integer.valueOf(req.getParameter("id"));
		Customer selectedCustomer = findCustomerById(id);
		resp.setContentType("text/html");
		resp.setCharacterEncoding("ISO-8859-1");
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("<!DOCTYPE html>");
		stringBuffer.append("<html>");
		{
			stringBuffer.append("<head>");
			{
				stringBuffer.append("<meta charset='ISO-8859-1'>");
				stringBuffer.append("<title>Customers</title>");
			}
			stringBuffer.append("</head>");
			stringBuffer.append("<body>");
			{
				stringBuffer.append("<form action='updateCustomer' method='post'>");
				{
					stringBuffer.append("<table>");
					{
						stringBuffer.append("<tr>");
						{
							stringBuffer.append("<td>First Name</td>");
							stringBuffer.append(String.format("<td><input type='text' name='firstName' value='%s'></td>", selectedCustomer.getFirstName()));
						}
						stringBuffer.append("</tr>");
						stringBuffer.append("<tr>");
						{
							stringBuffer.append("<td>Last Name</td>");
							stringBuffer.append(String.format("<td><input type='text' name='lastName' value='%s'></td>", selectedCustomer.getLastName()));
						}
						stringBuffer.append("</tr>");
						stringBuffer.append("</tr>");
						stringBuffer.append("<tr>");
						{
							stringBuffer.append("<td></td>");
							stringBuffer.append("<td><input type='submit' value='Save'></td>");
						}
						stringBuffer.append("</tr>");
					}
					stringBuffer.append("</table>");
					stringBuffer.append(String.format("<input type='hidden' name='id' value='%d'>", selectedCustomer.getId()));
				}
				stringBuffer.append("</form>");
			}
			stringBuffer.append("</body>");
		}
		stringBuffer.append("</html>");
		resp.getWriter().print(stringBuffer.toString());
	}

	private Customer findCustomerById(int id) {
		Customer customer = null;
		for (Customer currentCustomer : customers) {
			if (currentCustomer.getId() == id) {
				customer = currentCustomer;
				break;
			}
		}
		return customer;
	}

	private void printShowCustomersPage(HttpServletResponse resp) throws IOException {
		resp.setContentType("text/html");
		resp.setCharacterEncoding("ISO-8859-1");
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("<!DOCTYPE html>");
		stringBuffer.append("<html>");
		{
			stringBuffer.append("<head>");
			{
				stringBuffer.append("<meta charset='ISO-8859-1'>");
				stringBuffer.append("<title>Customers</title>");
				stringBuffer.append("<link rel='stylesheet' href='http://yui.yahooapis.com/pure/0.5.0/pure-min.css'>");
			}
			stringBuffer.append("</head>");
			stringBuffer.append("<body>");
			{
				stringBuffer.append("<table class='pure-table'>");
				{
					stringBuffer.append("<thead>");
					stringBuffer.append("<tr>");
					{
						stringBuffer.append("<th>ID</th>");
						stringBuffer.append("<th>First Name</th>");
						stringBuffer.append("<th>Last Name</th>");
					}
					stringBuffer.append("</tr>");
					stringBuffer.append("</thead>");
					stringBuffer.append("<tbody>");
					for (int i = 0; i < customers.size(); i++) {
						Customer customer = customers.get(i);
						if (i % 2 == 1) {
							stringBuffer.append("<tr class='pure-table-odd'>");
						} else {
							stringBuffer.append("<tr>");
						}
						{
							stringBuffer.append(String.format("<td>%d</td>", customer.getId()));
							stringBuffer.append(String.format("<td>%s</td>", customer.getFirstName()));
							stringBuffer.append(String.format("<td>%s</td>", customer.getLastName()));
							stringBuffer.append(String.format("<td><a href='editCustomer?id=%d'>Edit</a></td>", customer.getId()));
						}
						stringBuffer.append("</tr>");
					}
					stringBuffer.append("</tbody>");
				}
				stringBuffer.append("</table>");
			}
			stringBuffer.append("</body>");
		}
		stringBuffer.append("</html>");
		resp.getWriter().print(stringBuffer.toString());
	}
}
